# 处理学生成绩CSV文件
import csv

# 学生成绩数据（姓名，语文，数学，英语，物理，化学）
students = [
    ["张三", 90, 85, 78, 92, 88],
    ["李四", 76, 88, 95, 80, 79],
    ["王五", 82, 91, 86, 85, 90],
    ["赵六", 95, 77, 88, 94, 85],
    ["孙七", 68, 82, 90, 76, 88]
]

# 写入CSV文件
with open("students.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.writer(f)
    writer.writerow(["姓名", "语文", "数学", "英语", "物理", "化学"])  # 标题行
    writer.writerows(students)

# 读取CSV文件并输出学生成绩及统计平均分
print("\n学生成绩明细：")
print("{:<5} {:<5} {:<5} {:<5} {:<5} {:<5}".format("姓名", "语文", "数学", "英语", "物理", "化学"))

sum_chinese = sum_math = sum_english = sum_physics = sum_chemistry = 0

with open("students.csv", "r", encoding="utf-8") as f:
    reader = csv.reader(f)
    next(reader)  # 跳过标题行
    for row in reader:
        name, chinese, math, english, physics, chemistry = row
        print("{:<5} {:<7} {:<7} {:<7} {:<7} {:<7}".format(
            name, chinese, math, english, physics, chemistry
        ))
        # 累加各科成绩
        sum_chinese += int(chinese)
        sum_math += int(math)
        sum_english += int(english)
        sum_physics += int(physics)
        sum_chemistry += int(chemistry)

# 计算平均分
avg_chinese = sum_chinese / 5
avg_math = sum_math / 5
avg_english = sum_english / 5
avg_physics = sum_physics / 5
avg_chemistry = sum_chemistry / 5

# 输出结果
print("\n学生成绩统计：")
print(f"语文平均分: {avg_chinese:.2f}")
print(f"数学平均分: {avg_math:.2f}")
print(f"英语平均分: {avg_english:.2f}")
print(f"物理平均分: {avg_physics:.2f}")
print(f"化学平均分: {avg_chemistry:.2f}")